<?php
class Link_m extends CI_Model{
	function __construct(){
		parent::__construct();
		$this->load->database();
	}
	
	//--count categories--//
	function count_categories(){
		$query = 
				$this->db->select()
						 ->from('link_category')
						 ->get();
		if($query->num_rows()>=0){
			return $query->num_rows();
		}else{
			return array();
		}
	}
	
	//--list categories--//
	function list_categories($per_page, $off_set){
		$query=
			$this->db->select()
					 ->from('link_category')
					 ->limit($per_page, $off_set)
					 ->get();
		if($query->num_rows()>0){
			return $query->result_array();
		}
	}
	
	//--add category--//
	function add_category($data){
		$query = 
			$this->db->insert("link_category", $data);
		return $query;	
	}	
	
	//--count categories by name--//
	function count_categories_by_name($name){
		$query = 
			$this->db->select()
					 ->from("link_category")
					 ->where("name", $name)
					 ->get();
		$rowCount = $query->num_rows();
		if($rowCount >= 0){
			return $rowCount;
		}else{
			return 0;
		}
	}
	
	//--get category details--//
	function get_category_details($id){
		$query = 
			 	$this->db->select()
			 			 ->from("link_category")
					     ->where("id", $id)
					     ->limit(1)
					     ->get();
		if($query -> num_rows() >0){
			return $query->row();
		}else{
			return FALSE;
		}
	}

	//--edit category details--//
	function edit_category($id, $data){
		$query = $this->db->update("link_category", $data, array("id"=>$id));
		return $query;
	}
	
	//--delete category--//
	function delete_category($id) {
        $this->db->delete('link', array('cat_id' => $id));
        return $this->db->delete('link_category', array('id' => $id));
    }
    
    //--count partner link--//

	function count_partner_link(){
		$query = 
				$this->db->select()
						 ->from('link')
						 ->where('kind', 'partner')
						 ->get();
		if($query->num_rows()>=0){
			return $query->num_rows();
		}else{
			return array();
		}
	}
	
	//--list partner link--//
	function list_partner_link($per_page, $off_set){
		$query=
			$this->db->select()
					  ->from('link')
					  ->where('kind', 'partner')
					 ->limit($per_page, $off_set)
					 ->get();
		if($query->num_rows()>0){
			return $query->result_array();
		}
	}
	
	//--add partner link--//
	
	function add_partner_link($data){
		$query = 
			$this->db->insert("link", $data);
		return $query;	
	}

	//--get details of partner link--//
	function get_details_partner_link($id){
		$query = 
			 	$this->db->select()
			 			 ->from("link")
					     ->where("id", $id)
					     ->where("kind", "partner")
					     ->limit(1)
					     ->get();
		if($query -> num_rows() >0){
			return $query->row();
		}else{
			return FALSE;
		}
	}
	
	//--edit partner link--//
	function update_partner_link($id, $data){
		$query = $this->db->update("link", $data, array("id"=>$id));
		return $query;
	}
	
	//--delete partner link--//
	function delete_partner_link($id){
		$query = $this->db->delete('link', array('id' => $id));
		if($query){
			return $query;
		} else{
			return FALSE;
		}
		
	}
	
	//--delete multi partner link--//
	function delete_multi_partner_links($list) {
        $this->db->where('id in ' . $list);
        $query = $this->db->delete('link');
        return $query;
    }
    
    //--count event schedule--//
	function count_event_schedule(){
		$query = 
				$this->db->select('link.*, link_category.id as catid, link_category.name')
						 ->from('link')
						 ->join('link_category', 'link.cat_id = link_category.id', 'left')
						 ->where('link.kind', 'event')
						 ->get();
		if($query->num_rows()>=0){
			return $query->num_rows();
		}else{
			return array();
		}
	}
	
	//--list event schedule--//
	function list_event_schedule($per_page, $off_set){
		$query=
			$this->db->select('link.*, link_category.id as catid, link_category.name as catname')
						 ->from('link')
						 ->join('link_category', 'link.cat_id = link_category.id', 'left')
						 ->where('link.kind', 'event')
						 ->limit($per_page, $off_set)
						 ->get();
		if($query->num_rows()>0){
			return $query->result_array();
		}
	}
	
	//--add event schedule--//
	
	function add_event_schedule($data){
		$query = 
			$this->db->insert("link", $data);
		return $query;	
	}
	
	//--check exist link--//
	function check_exist_link($link, $kind){
		if($kind == "partner"){
			$query = $this->db->select()
			 			      ->from("link")
			 			      ->where("link", $link)
			 			      ->where('kind', 'partner')
			 			      ->get();
			$count = $query->num_rows();
			return $count;
		}
		elseif($kind == "event"){
			$query=
			$this->db->select('link.*, link_category.id as catid, link_category.name as catname')
						 ->from('link')
						 ->join('link_category', 'link.cat_id = link_category.id', 'left')
						 ->where('link.kind', 'event')
						 ->where('link.link', $link)
						 ->get();
			$count = $query->num_rows();
			return $count;	
		}else{
			return FALSE;
		}
	}
	
	//--get details of event schedule--//
	function get_details_event($id){
		$query=
			$this->db->select('link.*, link_category.id as catid, link_category.name as catname')
						 ->from('link')
						 ->join('link_category', 'link.cat_id = link_category.id', 'left')
						 ->where('link.kind', 'event')
						 ->where('link.id', $id)
						 ->limit(1)
						 ->get();
		if($query -> num_rows() >0){
			return $query->row();
		}else{
			return FALSE;
		}
	}
	
	//--edit event schedule--//
	function update_event($id, $data){
		$query = $this->db->update("link", $data, array("id"=>$id));
		return $query;
	}
	
	//--delete event schedule--//
	function delete_event($id){
		$query = $this->db->delete('link', array('id' => $id));
		if($query){
			return $query;
		} else{
			return FALSE;
		}
		
	}
	
	//--delete multi event schedule--//
	function delete_multi_events($list) {
        $this->db->where('id in ' . $list);
        $query = $this->db->delete('link');
        return $query;
    }
    
    //--add event schedule description--//
	function add_event_description($data){
		$query = 
			$this->db->insert("event_description", $data);
		return $query;	
	}	
	
	//--get event schedulde description--//
	function get_event_description(){
		$query = $this->db->select()
						  ->from("event_description")
						  ->order_by("id", "ASC")
						  ->limit(1)
						  ->get();
						  
		if($query -> num_rows() > 0){
			return $query->row();
		}else{
			return FALSE;
		}
	}
	
	//--edit event description--//
	function edit_event_description($id, $data){
		$query = $this->db->update("event_description", $data, array("id"=>$id));
		return $query;
	}	
}